<?php
class func {
	function __construct() {
	}
	//截取字符串
	function subAddress($addr, $num = 42) {
		$len = strlen ( $addr );
		$suffix = '';
		if ($len > $num) {
			$len = $num;
			$suffix = '...';
			for($idx = 0; $idx < $len;) {
				if (ord ( $addr [$idx] ) > 0x7f) {
					$idx += 3;
				} else {
					$idx ++;
				}
			}
			return substr ( $addr, 0, $idx ) . $suffix;
		} else
			return $addr;
	}
	//加密解密
	function encrypt($s, $key) {
		$r = '';
		for($i = 0; $i <= strlen ( $s ); $i ++)
			$r .= substr ( str_shuffle ( md5 ( $key ) ), ($i % strlen ( md5 ( $key ) )), 1 ) . $s [$i];
		for($i = 1; $i <= strlen ( $r ); $i ++)
			$s [$i - 1] = chr ( ord ( $r [$i - 1] ) + ord ( substr ( md5 ( $key ), ($i % strlen ( md5 ( $key ) )) - 1, 1 ) ) );
		return urlencode ( base64_encode ( $s ) );
	}
	function decrypt($s, $key) {
		$r = '';
		$s = base64_decode ( urldecode ( $s ) );
		for($i = 1; $i <= strlen ( $s ); $i ++)
			$s [$i - 1] = chr ( ord ( $s [$i - 1] ) - ord ( substr ( md5 ( $key ), ($i % strlen ( md5 ( $key ) )) - 1, 1 ) ) );
		for($i = 1; $i <= strlen ( $s ) - 2; $i = $i + 2)
			$r .= $s [$i];
		return $r;
	}
	//身份验证、获得年龄
	function getAge($noid) {
		$byear = substr ( $noid, 6, 4 ); //取得出生年月
		$bmon = substr ( $noid, 10, 2 ); //取得出生月份
		$bday = substr ( $noid, 12, 2 ); //判断出生日期
		$dateStr = $byear . "-" . $bmon . "-" . $bday;
		list ( $by, $bm, $bd ) = explode ( '-', $dateStr );
		$cm = date ( 'n' );
		$cd = date ( 'j' );
		$age = date ( 'Y' ) - $by - 1;
		if ($cm > $bm || $cm = $bm && $cd > $$bd)
			$age ++;
		return $age;
	}
	function validation_filter_id_card($id_card) {
		if (strlen ( $id_card ) == 18) {
			return $this->idcard_checksum18 ( $id_card );
		} elseif ((strlen ( $id_card ) == 15)) {
			$id_card = $this->idcard_15to18 ( $id_card );
			return $this->idcard_checksum18 ( $id_card );
		} else {
			return false;
		}
	}
	// 计算身份证校验码，根据国家标准GB 11643-1999   
	function idcard_verify_number($idcard_base) {
		if (strlen ( $idcard_base ) != 17) {
			return false;
		}
		//加权因子   
		$factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
		//校验码对应值   
		$verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' );
		$checksum = 0;
		for($i = 0; $i < strlen ( $idcard_base ); $i ++) {
			$checksum += substr ( $idcard_base, $i, 1 ) * $factor [$i];
		}
		$mod = $checksum % 11;
		$verify_number = $verify_number_list [$mod];
		return $verify_number;
	}
	// 将15位身份证升级到18位   
	function idcard_15to18($idcard) {
		if (strlen ( $idcard ) != 15) {
			return false;
		} else {
			// 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码   
			if (array_search ( substr ( $idcard, 12, 3 ), array ('996', '997', '998', '999' ) ) !== false) {
				$idcard = substr ( $idcard, 0, 6 ) . '18' . substr ( $idcard, 6, 9 );
			} else {
				$idcard = substr ( $idcard, 0, 6 ) . '19' . substr ( $idcard, 6, 9 );
			}
		}
		$idcard = $idcard . idcard_verify_number ( $idcard );
		return $idcard;
	}
	// 18位身份证校验码有效性检查   
	function idcard_checksum18($idcard) {
		if (strlen ( $idcard ) != 18) {
			return false;
		}
		$idcard_base = substr ( $idcard, 0, 17 );
		if ($this->idcard_verify_number ( $idcard_base ) != strtoupper ( substr ( $idcard, 17, 1 ) )) {
			return false;
		} else {
			return true;
		}
	}
	//获得当前脚本
	function GetCurUrl() {
		if (! empty ( $_SERVER ["REQUEST_URI"] )) {
			$scriptName = $_SERVER ["REQUEST_URI"];
			$nowurl = $scriptName;
		} else {
			$scriptName = $_SERVER ["PHP_SELF"];
			if (empty ( $_SERVER ["QUERY_STRING"] )) {
				$nowurl = $scriptName;
			} else {
				$nowurl = $scriptName . "?" . $_SERVER ["QUERY_STRING"];
			}
		}
		return $nowurl;
	}
	//HTML转文本
	function Html2Text($str, $r = 0) {
		if (! function_exists ( 'SpHtml2Text' )) {
			function SpHtml2Text($str) {
				$str = preg_replace ( "/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU", "", $str );
				$alltext = "";
				$start = 1;
				for($i = 0; $i < strlen ( $str ); $i ++) {
					if ($start == 0 && $str [$i] == ">") {
						$start = 1;
					} else if ($start == 1) {
						if ($str [$i] == "<") {
							$start = 0;
							$alltext .= " ";
						} else if (ord ( $str [$i] ) > 31) {
							$alltext .= $str [$i];
						}
					}
				}
				$alltext = str_replace ( "　", " ", $alltext );
				$alltext = preg_replace ( "/&([^;&]*)(;|&)/", "", $alltext );
				$alltext = preg_replace ( "/[ ]+/s", " ", $alltext );
				return $alltext;
			}
		}
		if ($r == 0) {
			return SpHtml2Text ( $str );
		} else {
			$str = SpHtml2Text ( stripslashes ( $str ) );
			return addslashes ( $str );
		}
	}
	
	//文本转HTML
	function Text2Html($txt) {
		$txt = str_replace ( "  ", "　", $txt );
		$txt = str_replace ( "<", "&lt;", $txt );
		$txt = str_replace ( ">", "&gt;", $txt );
		$txt = preg_replace ( "/[\r\n]{1,}/isU", "<br/>\r\n", $txt );
		return $txt;
	}
	//获得IP
	function GetIP() {
		if (! empty ( $_SERVER ["HTTP_CLIENT_IP"] )) {
			$cip = $_SERVER ["HTTP_CLIENT_IP"];
		} else if (! empty ( $_SERVER ["HTTP_X_FORWARDED_FOR"] )) {
			$cip = $_SERVER ["HTTP_X_FORWARDED_FOR"];
		} else if (! empty ( $_SERVER ["REMOTE_ADDR"] )) {
			$cip = $_SERVER ["REMOTE_ADDR"];
		} else {
			$cip = '';
		}
		preg_match ( "/[\d\.]{7,15}/", $cip, $cips );
		$cip = isset ( $cips [0] ) ? $cips [0] : 'unknown';
		unset ( $cips );
		return $cip;
	}
	//cookie
	function PutCookie($key, $value, $kptime = 3600, $pa = "/") {
		//global $cfg_cookie_encode;
		setcookie ( $key, $value, time () + $kptime, $pa);
		//setcookie ( $key . '__ckMd5', substr ( md5 ( $cfg_cookie_encode . $value ), 0, 16 ), time () + $kptime, $pa );
	}
	
	function DropCookie($key) {
		setcookie ( $key, '', time () - 360000, "/" );
		setcookie ( $key . '__ckMd5', '', time () - 360000, "/" );
	}
	
	function GetCookie($key) {return $_COOKIE [$key];
		global $cfg_cookie_encode;
		if (! isset ( $_COOKIE [$key] ) || ! isset ( $_COOKIE [$key . '__ckMd5'] )) {
			return '';
		} else {
			if ($_COOKIE [$key . '__ckMd5'] != substr ( md5 ( $cfg_cookie_encode . $_COOKIE [$key] ), 0, 16 )) {
				return '';
			} else {
				return $_COOKIE [$key];
			}
		}
	}
	// $rptype = 0 表示仅替换 html标记
	// $rptype = 1 表示替换 html标记同时去除连续空白字符
	// $rptype = 2 表示替换 html标记同时去除所有空白字符
	// $rptype = -1 表示仅替换 html危险的标记
	function HtmlReplace($str, $rptype = 0) {
		$str = stripslashes ( $str );
		if ($rptype == 0) {
			$str = htmlspecialchars ( $str );
		} else if ($rptype == 1) {
			$str = htmlspecialchars ( $str );
			$str = str_replace ( "　", ' ', $str );
			$str = ereg_replace ( "[\r\n\t ]{1,}", ' ', $str );
		} else if ($rptype == 2) {
			$str = htmlspecialchars ( $str );
			$str = str_replace ( "　", '', $str );
			$str = ereg_replace ( "[\r\n\t ]", '', $str );
		} else {
			$str = ereg_replace ( "[\r\n\t ]{1,}", ' ', $str );
			$str = eregi_replace ( 'script', 'ｓｃｒｉｐｔ', $str );
			$str = eregi_replace ( "<[/]{0,1}(link|meta|ifr|fra)[^>]*>", '', $str );
		}
		return addslashes ( $str );
	}
	//错误信息
	function ParamError() {
		$this->ShowMsg ( '对不起，你输入的参数有误！', 'javascript:;' );
		exit ();
	}
	function ShowMsg($msg, $gourl, $onlymsg = 0, $limittime = 0) {
		if (empty ( $GLOBALS ['cfg_phpurl'] ))
			$GLOBALS ['cfg_phpurl'] = '..';
		
		$htmlhead = "<html>\r\n<head>\r\n<title>提示信息</title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n";
		$htmlhead .= "<base target='_self'/>\r\n<style>div{line-height:160%;}</style></head>\r\n<body leftmargin='0' topmargin='0'>" . (isset ( $GLOBALS ['ucsynlogin'] ) ? $GLOBALS ['ucsynlogin'] : '') . "\r\n<center>\r\n<script>\r\n";
		$htmlfoot = "</script>\r\n</center>\r\n</body>\r\n</html>\r\n";
		
		$litime = ($limittime == 0 ? 1000 : $limittime);
		$func = '';
		
		if ($gourl == '-1') {
			if ($limittime == 0)
				$litime = 5000;
			$gourl = "javascript:history.go(-1);";
		}
		
		if ($gourl == '' || $onlymsg == 1) {
			$msg = "<script>alert(\"" . str_replace ( "\"", "“", $msg ) . "\");</script>";
		} else {
			//当网址为:close::objname 时, 关闭父框架的id=objname元素
			if (eregi ( 'close::', $gourl )) {
				$tgobj = trim ( eregi_replace ( 'close::', '', $gourl ) );
				$gourl = 'javascript:;';
				$func .= "window.parent.document.getElementById('{$tgobj}').style.display='none';\r\n";
			}
			
			$func .= "      var pgo=0;
      function JumpUrl(){
        if(pgo==0){ location='$gourl'; pgo=1; }
      }\r\n";
			$rmsg = $func;
			$rmsg .= "document.write(\"<br /><div style='width:450px;padding:0px;border:1px solid #D1DDAA;'>";
			$rmsg .= "<div style='padding:6px;font-size:12px;border-bottom:1px solid #D1DDAA;background:#DBEEBD url({$GLOBALS['cfg_phpurl']}/img/wbg.gif)';'><b>提示信息！</b></div>\");\r\n";
			$rmsg .= "document.write(\"<div style='height:130px;font-size:10pt;background:#ffffff'><br />\");\r\n";
			$rmsg .= "document.write(\"" . str_replace ( "\"", "“", $msg ) . "\");\r\n";
			$rmsg .= "document.write(\"";
			
			if ($onlymsg == 0) {
				if ($gourl != 'javascript:;' && $gourl != '') {
					$rmsg .= "<br /><a href='{$gourl}'>如果你的浏览器没反应，请点击这里...</a>";
					$rmsg .= "<br/></div>\");\r\n";
					$rmsg .= "setTimeout('JumpUrl()',$litime);";
				} else {
					$rmsg .= "<br/></div>\");\r\n";
				}
			} else {
				$rmsg .= "<br/><br/></div>\");\r\n";
			}
			$msg = $htmlhead . $rmsg . $htmlfoot;
		}
		echo $msg;
	}
	//过滤用于搜索的字符串
	function FilterSearch($keyword) {
		global $cfg_soft_lang;
		if ($cfg_soft_lang == 'utf-8') {
			$keyword = ereg_replace ( "[\"\r\n\t\$\\><']", '', $keyword );
			if ($keyword != stripslashes ( $keyword )) {
				return '';
			} else {
				return $keyword;
			}
		} else {
			$restr = '';
			for($i = 0; isset ( $keyword [$i] ); $i ++) {
				if (ord ( $keyword [$i] ) > 0x80) {
					if (isset ( $keyword [$i + 1] ) && ord ( $keyword [$i + 1] ) > 0x40) {
						$restr .= $keyword [$i] . $keyword [$i + 1];
						$i ++;
					} else {
						$restr .= ' ';
					}
				} else {
					if (eregi ( "[^0-9a-z@#\.]", $keyword [$i] )) {
						$restr .= ' ';
					} else {
						$restr .= $keyword [$i];
					}
				}
			}
		}
		return $restr;
	}
	//邮箱格式检查
	function CheckEmail($email) {
		return eregi ( "^[0-9a-z][a-z0-9\._-]{1,}@[a-z0-9-]{1,}[a-z0-9]\.[a-z\.]{1,}[a-z]$", $email );
	}
	//url获得数据string
	function WriteFile($url,$filename,$key) {
		$data = file_get_contents ( $url );
		$array_data = unserialize ( $data );
		$data = $this->encrypt ( $data, $key );
		file_put_contents ( "data/txt/".$filename.".txt", "time=" . time () . "\n" . "data=" . $data );
	}
	function getFile($filename,$key) {
		$filedata = parse_ini_file ( "data/txt/".$filename.".txt", true );
		$ret [] = array_shift ( $filedata );
		$ret [] = unserialize ( $this->decrypt ( array_shift ( $filedata ), $key ) );
		return $ret;
	}
}
?>